﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;
using System.Data;
using System.Text;
using Gift.DAL;
using Gift.Model;

namespace WebSite.cn
{
    public partial class index : BasePage
    {
        public StringBuilder sbFlashHtml = new StringBuilder();
        public StringBuilder sbProfessionHtml = new StringBuilder();
        public StringBuilder sProductHtml = new StringBuilder();
        public StringBuilder sMarketProductHtml = new StringBuilder();
        public StringBuilder sCardProductHtml = new StringBuilder();

        DALProfessionAreaType theAreaType = new DALProfessionAreaType();
        DALShoppro theShoppro = new DALShoppro();

        StringBuilder _newsNotice = new StringBuilder();

        int iFloorCount = 1;
        string sFloorBg = "t_name_bg;";

        string sBodyAd1 = "";

        public string SBodyAd1
        {
            get { return sBodyAd1; }
        }
        string sBodyAd2 = "";

        public string SBodyAd2
        {
            get { return sBodyAd2; }
        }
        string sBodyAd3 = "";

        public string SBodyAd3
        {
            get { return sBodyAd3; }
        }
        string sBodyAd4 = "";

        public string SBodyAd4
        {
            get { return sBodyAd4; }
        }


        public StringBuilder NewsNotice
        {
          get { return _newsNotice; }

        }

        StringBuilder _newsIndustry = new StringBuilder();

        public StringBuilder NewsIndustry
        {
          get { return _newsIndustry; }
          set { _newsIndustry = value; }
        }



        // Methods
        private void Bind()
        {
            DataTable table = SQLAccess.GreatDs("select isnull(flashurl,'') as flashurl,isnull(flashname,'') as flashname,isnull(flashpic,'') as flashpic from DB_Flash order by flashid asc").Tables[0];
            if (table.Rows.Count > 0)
            {
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    if (i < table.Rows.Count )
                    {
                        if (table.Rows[i]["flashurl"] == null || table.Rows[i]["flashurl"].ToString() == "")
                        {
                            sbFlashHtml.AppendFormat("<a><img src=\"{0}\" width=\"780\" height=\"280\" alt=\"{1}\" /></a>"
                                                     , "../uploadfile/" + table.Rows[i]["flashpic"].ToString().Replace(@"\", "/")
                                                     , table.Rows[i]["flashname"].ToString());
                        }
                        else
                        {
                            sbFlashHtml.AppendFormat("<a href='{2}' target='_blank'><img src=\"{0}\" width=\"780\" height=\"280\" alt=\"{1}\" /></a>"
                                                    , "../uploadfile/" + table.Rows[i]["flashpic"].ToString().Replace(@"\", "/")
                                                    , table.Rows[i]["flashname"].ToString()
                                                    , table.Rows[i]["flashurl"].ToString());
                        }
                    }
             
                }
            }
            table = SQLAccess.GreatDs(string.Concat(new object[] { "select top ", Convert.ToInt32(info.info1("", "homeshownnum")), " newsid,newsname,typeid from DB_News where typeid in(", "select classid from db_newsclass where classname='资讯中心'", ") and visible=1 and isnew=1 order by newsorder,updatetime desc" })).Tables[0];
            if (table.Rows.Count > 0)
            {
                foreach (DataRow row in table.Rows)
                {
                   _newsNotice.AppendFormat("<li><a href=\"{0}\" title=\"{1}\">{1}</a></li>"
                                            , "/cn/news_01.aspx?tid=" + row["typeid"].ToString() + "&nid=" + row["newsid"].ToString()
                                            ,  row["newsname"].ToString() 
                                            , fun.Left(row["newsname"].ToString(), 10) + "..</a></li>"
                                            );
                }
            }

                 table = SQLAccess.GreatDs(string.Concat(new object[] { "select top ", Convert.ToInt32(info.info1("", "homeshownnum")), " newsid,newsname,typeid from DB_News where typeid in(", "select classid from db_newsclass where classname='行业新闻'", ") and visible=1 and isnew=1 order by newsorder,updatetime desc" })).Tables[0];
            if (table.Rows.Count > 0)
            {
                foreach (DataRow row in table.Rows)
                {
                    _newsIndustry.AppendFormat("<li><a href=\"{0}\" title=\"{1}\">{1}</a></li>"
                                            , "/cn/news_01.aspx?tid=" + row["typeid"].ToString() + "&nid=" + row["newsid"].ToString()
                                            ,  row["newsname"].ToString() 
                                            , fun.Left(row["newsname"].ToString(), 10) + "..</a></li>"
                                            );
                }
            }

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Session["backHref"] = Request.Url.PathAndQuery;
            if (!base.IsPostBack)
            {
          
                this.Bind();
                this.BindBodyAd();
                this.GetLeftProfessionMenu();
                this.BindProfessionProductList();
                this.BindMarketProductList();
                this.BindCardProductList();
            }
        }

        void GetLeftProfessionMenu()
        {
            List<tbProfessionAreaType> lstType = theAreaType.GetIndexMenuList(10);
            foreach (tbProfessionAreaType oType in lstType)
            {

                sbProfessionHtml.AppendFormat("<div class=\"cate_item\"><div class=\"cate_title\"><h3><strong><a href=\"{0}\">{1}</a></strong></h3><em></em></div></div>"
                                              , "professionArea.aspx?sid=2" + "&typeid=" + oType.Id.ToString()
                                              , oType.Name
                                              );
            }
        }

        void BindProfessionProductList()
        {

            iFloorCount = 1;
              List<tbProfessionAreaType> lstType = theAreaType.GetIndexMenuList(10);
              List<tbProfessionAreaType> lstTypeMenu = theAreaType.GetIndexMenuList(5);
             foreach (tbProfessionAreaType oType in lstType)
            {

                DataTable dtProduct = theShoppro.GetListForProfession(oType.Id.ToString(), 5,true).Tables[0];
                if (dtProduct == null || dtProduct.Rows.Count <= 0) continue;

                sProductHtml.Append("<div class=\"new_por\"><div class=\"title\"><div class=\"pro_menu\">");
                foreach (tbProfessionAreaType oTypeIn in lstTypeMenu)
                {
                    sProductHtml.AppendFormat("<a href=\"{0}\" target=\"_self\">{1}</a> "
                                               , "/cn/professionArea.aspx?sid=2" + "&typeid=" + oTypeIn.Id.ToString()
                                               , oTypeIn.Name
                                               );
                }
                sProductHtml.AppendFormat("<a href=\"{0}\" target=\"_self\">{1}</a> "
                                                 , "/cn/professionArea.aspx?sid=2"
                                                 , "更多>></div>"
                
                                                 );
                if (iFloorCount % 3 == 0) sFloorBg = "t_name_bg3";
                else if (iFloorCount % 2 == 0) sFloorBg = "t_name_bg2";
               else
                   sFloorBg = "t_name_bg";
              sProductHtml.AppendFormat("<p><span class=\"t_name_1 {3}\">{0}</span></p></div><div class=\"info\"><textarea class=\"lazyload\">"
                              + "<a href=\"{1}\" target=\"_self\" class=\"bannerClassimg\"><img src=\"{2}\" alt=\"\" width=\"203\" height=\"203\"/></a>"
                              , iFloorCount.ToString() + "F  " + oType.Name
                              , "/cn/professionArea.aspx?sid=2" + "&typeid=" + oType.Id.ToString()
                              , "/uploadfile/productClass/" + oType.Pic
                              , sFloorBg
                              );
              iFloorCount++;
              sProductHtml.Append("<div class=\"info_list\"><ul>");
              string sProName = string.Empty;
                 foreach(DataRow dr in dtProduct.Rows)
                 {
                     sProName = dr["proName"].ToString().Length > 12 ? dr["proName"].ToString().Substring(0, 12) + ".." : dr["proName"].ToString();
                      sProductHtml.AppendFormat("<li>"
                                      +" <a href=\"{0}\" target=\"_self\" name=\"#hoverinfo{1}\" class=\"pro_img\"><img width=\"130\" height=\"130\" alt=\"\" src=\"{2}\" /></a>"
                                      + " <p><a href=\"{0}\" target=\"_blank\" title=\"{7}\">{3}</a><em>库存：{4}</em><em>兑换价：￥{5}</em></p>"
                                      + " <div id=\"hoverinfo{1}\" style=\"display: none;\"><img width=\"200\" height=\"200\" src=\"{2}\" alt=\"{7}\"/><p><strong>{7}</strong> <span>产品编号:{6}</span> <span><em>库存：{4}</em></span><span><em>兑换价￥{5}</em></span></p></div>"
                                      +"</li>"
                                      ,  "/cn/products_details.aspx?pid=" + dr["proid"].ToString() + "&sid=" + oType.Id.ToString()
                                      , dr["proid"].ToString()
                                      ,"/uploadfile/product/" + dr["propic2"].ToString()
                                      , sProName
                                      , dr["proku1"].ToString()
                                      , String.Format("{0:f}",dr["proprice1"].ToString())
                                      , dr["procode"].ToString()
                                      , dr["proName"].ToString()
                                      );
                 }

                 sProductHtml.Append("</ul><div class=\"clear\"></div></div><script type=\"text/javascript\">    $(function () { $(\"a.pro_img\").tooltip({ bodyHandler: function () { return $($(this).attr(\"name\")).html(); }, showURL: false }); });</script></textarea><div style=\"height:203px;\" class=\"div_lazyload_32\"></div></div></div>");
             }

        }

        void BindMarketProductList()
        {
            int sid = 4;
            DataTable dtClass = theShoppro.GetMarketCardMenu(sid, 5);
            DataTable dtSubClass;

            foreach (DataRow dr in dtClass.Rows)
            {

                dtSubClass = theShoppro.GetMarketCardSubMenu(sid, 8);
                DataTable dtProduct = theShoppro.GetListForClassId(dr["classid"].ToString(), 5,true).Tables[0];
                if (dtProduct == null || dtProduct.Rows.Count <= 0) continue;

                sMarketProductHtml.Append("<div class=\"new_por\"><div class=\"title\"><div class=\"pro_menu\">");
                foreach (DataRow drIn in dtSubClass.Rows)
                {
                    sMarketProductHtml.AppendFormat("<a href=\"{0}\" target=\"_self\">{1}</a> "
                                               , "/cn/hotpro.aspx?sid=" + sid + "&typeid=" + drIn["classId"].ToString()
                                               , drIn["className"].ToString()
                                               );
                }
                sMarketProductHtml.AppendFormat("<a href=\"{0}\" target=\"_self\">{1}</a> "
                                                 , "/cn/hotpro.aspx?sid=" + sid
                                                 , "更多>></div>"
                                                 );
                if (iFloorCount % 3 == 0) sFloorBg = "t_name_bg3";
                else if (iFloorCount % 2 == 0) sFloorBg = "t_name_bg2";
                else
                    sFloorBg = "t_name_bg";
                sMarketProductHtml.AppendFormat("<p><span class=\"t_name_1 {3}\">{0}</span></p></div><div class=\"info\"><textarea class=\"lazyload\">"
                                + "<a href=\"{1}\" target=\"_self\" class=\"bannerClassimg\"><img src=\"{2}\" alt=\"\" width=\"203\" height=\"203\"/></a>"
                                , iFloorCount.ToString() + "F " + dr["className"].ToString()
                                , "/cn/hotpro.aspx?sid=" + sid + "&typeid=" + dr["classId"].ToString()
                                , "/uploadfile/productClass/" + dr["classPic"].ToString()
                                , sFloorBg
                                );
                iFloorCount++;
                sMarketProductHtml.Append("<div class=\"info_list\"><ul>");
                string sProName = string.Empty;
                foreach (DataRow drSub in dtProduct.Rows)
                {
                    sProName = drSub["proName"].ToString().Length > 12 ? drSub["proName"].ToString().Substring(0, 12) + ".." : drSub["proName"].ToString();
                    sMarketProductHtml.AppendFormat("<li>"
                                    + " <a href=\"{0}\" target=\"_self\" name=\"#hoverinfo{1}\" class=\"pro_img\"><img width=\"130\" height=\"130\" alt=\"\" src=\"{2}\" /></a>"
                                    + " <p><a href=\"{0}\" target=\"_blank\" title=\"{7}\">{3}</a><em>库存：{4}</em><em>零售价：￥{5}</em></p>"
                                    + " <div id=\"hoverinfo{1}\" style=\"display: none;\"><img width=\"200\" height=\"200\" src=\"{2}\" alt=\"{7}\"/><p><strong>{7}</strong> <span>产品编号:{6}</span> <span><em>库存：{4}</em></span><span><em>兑换价￥{5}</em></span></p></div>"
                                    + "</li>"
                                    , "/cn/products_details.aspx?pid=" + drSub["proid"].ToString() + "&sid=" + sid
                                    , drSub["proid"].ToString()
                                    , "/uploadfile/product/" + drSub["propic2"].ToString()
                                    , sProName
                                    , drSub["proku1"].ToString()
                                    , String.Format("{0:f}", drSub["proprice1"].ToString())
                                    , drSub["procode"].ToString()
                                    , drSub["proName"].ToString()
                                    );
                }

                sMarketProductHtml.Append("</ul><div class=\"clear\"></div></div><script type=\"text/javascript\">    $(function () { $(\"a.pro_img\").tooltip({ bodyHandler: function () { return $($(this).attr(\"name\")).html(); }, showURL: false }); });</script></textarea><div style=\"height:203px;\" class=\"div_lazyload_32\"></div></div></div>");
            }

        }

        void BindBodyAd()
        {
            string str3 = "select topicid,pic,picurl,visible from DB_topic where isnew=1 order by topicorder asc";
           DataTable table3 = SQLAccess.GreatDs(str3).Tables[0];
            if (table3.Rows.Count > 1 && table3.Rows[1]["visible"].ToString().ToLower() == "true")
            {
                this.sBodyAd1 = "<div class=\"banner_ad\"><a href='" + table3.Rows[1]["picurl"].ToString()  + "' target='_blank'><img src='" + table3.Rows[1]["pic"].ToString() + "' border='0' /></a></div>";
            }
            if (table3.Rows.Count > 2 && table3.Rows[2]["visible"].ToString().ToLower() == "true")
            {
                this.sBodyAd2 = "<div class=\"banner_ad\"><a href='" + table3.Rows[2]["picurl"].ToString()  + "' target='_blank'><img  src='" + table3.Rows[2]["pic"].ToString() + "' border='0' /></a></div>";
            }
            if (table3.Rows.Count > 3 && table3.Rows[3]["visible"].ToString().ToLower() == "true")
            {
                this.sBodyAd3 = "<div class=\"banner_ad\"><a href='" + table3.Rows[3]["picurl"].ToString() + "' target='_blank'><img  src='" + table3.Rows[3]["pic"].ToString() + "' border='0' /></a></div>";
            }
            if (table3.Rows.Count > 4 && table3.Rows[4]["visible"].ToString().ToLower() == "true")
            {
                this.sBodyAd4 = "<div class=\"banner_ad\"><a href='" + table3.Rows[4]["picurl"].ToString()  + "' target='_blank'><img  src='" + table3.Rows[4]["pic"].ToString() + "' border='0' /></a></div>";
            }
        }

        void BindCardProductList()
        {

            int sid = 3;
            DataTable dtClass = theShoppro.GetMarketCardMenu(sid, 5);
            DataTable dtSubClass;

            foreach (DataRow dr in dtClass.Rows)
            {

                dtSubClass = theShoppro.GetMarketCardSubMenu(sid, 8);
                DataTable dtProduct = theShoppro.GetListForClassId(dr["classid"].ToString(), 5,true).Tables[0];
                if (dtProduct == null || dtProduct.Rows.Count <= 0) continue;

                sCardProductHtml.Append("<div class=\"new_por\"><div class=\"title\"><div class=\"pro_menu\">");
                foreach (DataRow drIn in dtSubClass.Rows)
                {
                    sCardProductHtml.AppendFormat("<a href=\"{0}\" target=\"_self\">{1}</a> "
                                               , "/cn/hotpro.aspx?sid=" + sid + "&typeid=" + drIn["classId"].ToString()
                                               , drIn["className"].ToString()
                                               );
                }
                sCardProductHtml.AppendFormat("<a href=\"{0}\" target=\"_self\">{1}</a> "
                                                 , "/cn/hotpro.aspx?sid=" + sid
                                                 , "更多>></div>"
                                                 );
                if (iFloorCount % 3 == 0) sFloorBg = "t_name_bg3";
                else if (iFloorCount % 2 == 0) sFloorBg = "t_name_bg2";
                else
                    sFloorBg = "t_name_bg";
                sCardProductHtml.AppendFormat("<p><span class=\"t_name_1 {3}\">{0}</span></p></div><div class=\"info\"><textarea class=\"lazyload\">"
                                + "<a href=\"{1}\" target=\"_self\" class=\"bannerClassimg\"><img src=\"{2}\" alt=\"\" width=\"203\" height=\"203\"/></a>"
                                , iFloorCount + "F " + dr["className"].ToString()
                                , "/cn/hotpro.aspx?sid=" + sid + "&typeid=" + dr["classId"].ToString()
                                , "/uploadfile/productClass/" + dr["classPic"].ToString()
                                , sFloorBg
                                );
                iFloorCount++;
                sCardProductHtml.Append("<div class=\"info_list\"><ul>");
                string sProName = string.Empty;
                foreach (DataRow drSub in dtProduct.Rows)
                {
                    sProName = drSub["proName"].ToString().Length > 12 ? drSub["proName"].ToString().Substring(0, 12) + ".." : drSub["proName"].ToString();
                    sCardProductHtml.AppendFormat("<li>"
                                    + " <a href=\"{0}\" target=\"_self\" name=\"#hoverinfo{1}\" class=\"pro_img\"><img width=\"130\" height=\"130\" alt=\"\" src=\"{2}\" /></a>"
                                    + " <p><a href=\"{0}\" target=\"_blank\" title=\"{7}\">{3}</a><em>库存：{4}</em><em>零售价：￥{5}</em></p>"
                                    + " <div id=\"hoverinfo{1}\" style=\"display: none;\"><img width=\"200\" height=\"200\" src=\"{2}\" alt=\"{7}\"/><p><strong>{7}</strong> <span>产品编号:{6}</span> <span><em>库存：{4}</em></span><span><em>兑换价￥{5}</em></span></p></div>"
                                    + "</li>"
                                    , "/cn/products_details.aspx?pid=" + drSub["proid"].ToString() + "&sid=" + sid
                                    , drSub["proid"].ToString()
                                    , "/uploadfile/product/" + drSub["propic2"].ToString()
                                    , sProName
                                    , drSub["proku1"].ToString()
                                    , String.Format("{0:f}", drSub["proprice1"].ToString())
                                    , drSub["procode"].ToString()
                                    , drSub["proName"].ToString()
                                    );
                }

                sCardProductHtml.Append("</ul><div class=\"clear\"></div></div><script type=\"text/javascript\">    $(function () { $(\"a.pro_img\").tooltip({ bodyHandler: function () { return $($(this).attr(\"name\")).html(); }, showURL: false }); });</script></textarea><div style=\"height:203px;\" class=\"div_lazyload_32\"></div></div></div>");
            }

        }

    }
}